package com.metagurukul.metaspace.dao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.metagurukul.metaspace.models.beans.Department;
import com.metagurukul.metaspace.util.Constants;

/**
 * @author ritesh
 * this class is for managing departments
 */
public class DepartmentDao extends DatabaseDAO {
	/**to get list of departments
	 * @return  list of departments
	 * @throws SQLException
	 */
	public List<Department> getRecordList ()
	throws SQLException {
		ArrayList<Department> departmentList = null;
		String query = "Select * from Department";
		try {
			getConnection();
			ps = con.prepareStatement(query);
			rs = ps.executeQuery();
			departmentList = new ArrayList<Department>();
			while (rs.next()) {
				Department department = new Department();
				department.setDeptId(rs.getInt("dept_id"));
				department.setDeptName(rs.getString("dept_name"));
				departmentList.add(department);
			}
		} catch (SQLException e) {
			throw new SQLException(Constants.SQL_QUERY_ERROR  + e);
		} finally {
			/* close connection */
			closeConnection();
		}
		return departmentList;
		
	}
	
	/**to get department name
	 * @param deptId id of department
	 * @return  name of department
	 * @throws SQLException
	 */
	public String getName(int deptId) throws SQLException{
		String query = "Select dept_name from Department where dept_id = ?"; // query
		try {
			getConnection();
			ps = con.prepareStatement(query);
			ps.setInt(1, deptId);
			rs = ps.executeQuery();
			if (rs.next()) {
				return rs.getString("dept_name");
			}
		} catch (SQLException e) {
			throw new SQLException(Constants.SQL_QUERY_ERROR  + e);
		} finally {
			/* close connection */
			closeConnection();
		}
		return null;
	}
}
